<?php
require_once("fckeditor/fckeditor.php");

class Page {

    // page data
    var $id = 0; // ID of the selected page
	var $dir      = 0; // directory ID of the selected page
	var $name = "";
	var $language = "";
	var $created = "";
	var $title = "";
	var $keywords = "";
	var $content = "";
	var $darkColor   = "#64b5db";
	var $lightColor  = "#badeef";
	var $noneColor   = "#ffffff";


    function init($id = 0) {
        global $admin;
	    
	    $str = "";
	    if ($id == 0)
	    {
	        $this->language = $admin->language;
	    }
	    elseif ($this->validPage($id, $admin->language))
	    {
	        $this->getPageData($id, $admin->language);
	    }
	    else
	    {
	        $str.= $this->error(_WRONGPAGEDATA);
		    $this->language = $admin->language;
	    }
	    
	    return $str;
    }


    function getPageData($id, $language) {
        global $table,$db;
	    
	    $result = $db->query("SELECT * FROM $table->page WHERE id='$id' AND language='$language'");
	    if ($db->numRows($result > 0))
	    {
	    	$row = $db->nextRecord ($result);
	    	$this->id       = $row->id;
	    	$this->dir      = $row->dir;
	    	$this->name     = $row->name;
	    	$this->language = $row->language;
			$this->title    = $row->title;
			$this->keywords = $row->keywords;
			$this->content  = $row->content;
	    }
    }


	function validPage ($id, $language) {
	    global $table,$db;
		
		if ($db->numRows($db->query("SELECT * FROM $table->page WHERE id='$id' AND language='$language'")) == 0)
		    { return false; }
		else
		{
		    return true;
		}
	}


	function done($text) {
	    return "<span class='green'><b>".$text."</b></span>";
	}


	function error($text) {
	    return "<span class='red'><b>".$text."</b></span>";
	}


    function renamePage($newname) {
	    global $table,$db;
		
		    if ($newname == "")
		    { // no name entered
		        return $this->error(_NOPAGENAME);
		    }
		    elseif ($db->numRows($db->query("SELECT * FROM $table->page WHERE name='$newname' AND dir='$this->dir' AND language='$this->language'")) > 0)
		    { // such directory name does already exist
		        return $this->error(_NEWPAGENAMEEXISTS);
		    }
		    else
		    { // executing the action - rename directory
		        if ($db->query("UPDATE $table->page SET name='$newname' WHERE id='$this->id' AND language='$this->language'"))
			    { // new directory was renamed
			        return $this->done(_PAGERENAMED);
			    }
			    else
			    { // action failed
			        return $this->error(_ACTIONFAILED);
			    }
		    }
	}


    function deletePage() {
	    global $table,$db;
		
		if ($this->validPage($this->id, $this->language))
		{ // executing the action - rename directory
            if ($db->query("DELETE FROM $table->page WHERE id='$this->id' AND language='$this->language'"))
		    { // the page was deleted
		        return $this->done(_PAGEDELETED);
		    }
		    else
		    { // action failed
		        return $this->error(_ACTIONFAILED);
		    }
		}
	}


    function showForm() {
	    global $table,$db,$html,$_GET,$_POST,$lang,$html_form,$admin;
		
		$str = "";
		
		if ($this->id > 0) { $string = _EDITPAGE; } else { $string = _CREATEPAGE; }
		$str.= $html->center("<b>$string</b><br><br>","adminmenu");
		
		$str.= $html_form->Begin(GLOBAL_SELF,"post","pagecreator");
		
		if ($this->id > 0)
		{
			$p =
			$html->tr(
			    $html->td(
				    _PATH.":"
				,2).
			    $html->td("<b>ROOT/".$this->getRoute($this->dir)."</b>"));
			$n =
			$html->tr(
			    $html->td(_NAME.":",2).
			    $html->td("<b>".$this->name."</b>")
			);
		}
		else
		{
			$p = 
			$html->tr(
			    $html->td(
				    _DIRECTORY.":"
				,2).
			    $html->td(
				    "<select name='dir' size='1'>".
					"<option value='0'>ROOT</option>".
    	       		$this->getDirs().
			        "</select>"
				)
			);
			$n =
			$html->tr(
			    $html->td(
				    _NAME.":"
				,2).
			    $html->td(
				    $html_form->addText("name", $this->name, 100, 255, "admin")
				)
			);
		}

		$str .= "<table width=850>".$p.$n."</table>";

		$oFCKeditor = new FCKeditor('pagecontent');
		$oFCKeditor->BasePath = '/fckeditor/';
		$oFCKeditor->Value = $this->content;
		$oFCKeditor->Width  = '840';
		$oFCKeditor->Height = '500';
		
		$str.=
		    $html->table(
				$html->tr(
				    $html->td(
					    _TITLE.":"
					,2).
				    $html->td(
					    $html_form->addText("title", $this->title, 100, 255, "admin")
					)
				).
				$html->tr(
				    $html->td(
					    _KEYWORDS.":"
					,2).
				    $html->td(
					    $html_form->addText("keywords", $this->keywords, 100, 255, "admin")
					)
				).
				$html->tr(
				    $html->td(
					    _CONTENT.":"
					,2).
				    $html->td(
					    $oFCKeditor->CreateHtml()
					)
				,1).
				$html->tr(
				    $html->td(
					    $html_form->addHidden("action","p:save:$this->id").
					    $html_form->addSubmit("submit",_SAVE)
					,1,"","","",2)
				).
				$html->tr(
				    $html->td().
				    $html->td(
					    "<iframe width='100%' height='90' frameborder='0' src='about:blank' name='help'></iframe>"
					,1,"","","",2)
				)
		    ,"",1,5,1).
		$html_form->End();

		return $str;
    }


    function getRoute($id = 0) {
	    global $table,$db,$admin;
	    
	    $str = "";
	    $result = $db->query("SELECT * FROM $table->dir WHERE id='$id' AND language='$admin->language'");
	    if ($db->numRows($result) > 0)
        {
	    	$row = $db->nextRecord($result);
	    	$str.= $this->getRoute($row->parent).$row->name."/";
	    }
	    
	    return $str;
	}


    function getDirs($parent = 0, $depth = 1) {
	    global $db,$table;
		
		$str = "";
		$result=$db->query("SELECT * FROM $table->dir WHERE parent='$parent' AND language='$this->language' ORDER BY name");
		while ($row = $db->nextRecord($result))
		{
		    $spaces = ""; for ($i=1; $i<=$depth; $i++) { $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; }
		    $str.= "<option value='$row->id'>".$spaces."$row->name</option>\n";
		    $str.= $this->getDirs($row->id, $depth+1);
		}
		return $str;
	}


    function saveNewPage($dir, $name, $title, $keywords, $pagecontent) {
	    global $db,$table,$html_form;
	    
		if ($name == "")
		{ 
		    $this->dir      = $dir;
		    $this->title    = $title;
			$this->keywords = $keywords;
			$this->content  = $pagecontent;
		    return $this->error(_NOPAGENAME).$this->showForm();
		}
		elseif ($db->numRows($db->query("SELECT * FROM $table->page WHERE dir='$dir' AND name='$name' AND language='$this->language'")))
		{
		    $this->dir      = $dir;
			$this->name     = $name;
		    $this->title    = $title;
			$this->keywords = $keywords;
			$this->content  = $pagecontent;
		    return $this->error(_NEWPAGENAMEEXISTS).$this->showForm();
		}
		else
		{
		    if ($db->query("INSERT INTO $table->page VALUES(
			               '',
						   '$dir',
						   '$name',
						   '$this->language',
						   '".date('YmdHis')."',
						   '$title',
						   '$keywords',
						   '$pagecontent'
						   )"))
			{ // new page was saved
			    $row = $db->nextRecord($db->query("SELECT * FROM $table->page WHERE dir='$dir' AND name='$name' AND language='$this->language'"));
			    return $this->done(_PAGESAVED).
				    "<br><br>".
				    $html_form->Begin(GLOBAL_SELF,"post","filemanager").
					$html_form->addHidden("action", "p:edit:$row->id").
					$html_form->addSubmit("submit", _EDITPAGE).
					$html_form->End().
					"<br><br>".
					"<a href='".GLOBAL_SELF."?op=d&id=$dir'>"._SHOWDIRECTORY."</a>";
			}
			else
			{ // action failed
			    return $this->error(_ACTIONFAILED);
			}
		}
	}


    function updatePage($id = 0, $title, $keywords, $pagecontent) {
	    global $db,$table,$html_form;
	    
		    if ($db->query("UPDATE $table->page SET
						   title='$title',
						   keywords='$keywords',
						   content='$pagecontent'
						   WHERE id='$id' AND language='$this->language'"))
			{ // new page was saved
			    return $this->done(_PAGESAVED).
				    "<br><br>".
				    $html_form->Begin(GLOBAL_SELF,"post","filemanager").
					$html_form->addHidden("action", "p:edit:$id").
					$html_form->addSubmit("submit", _EDITPAGE).
					$html_form->End().
					"<br><br>".
					"<a href='".GLOBAL_SELF."?op=d&id=$this->dir'>"._SHOWDIRECTORY."</a>";
			}
			else
			{ // action failed
			    return $this->error(_ACTIONFAILED);
			}
	}

}
?>